<?php

namespace Yearnfar\Bundle\CmsBundle\Controller;

use Doctrine\ORM\Tools\Pagination\Paginator;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Yearnfar\Bundle\CmsBundle\Entity\h5game;

/**
 * @Route("/h5game")
 * @Template()
 */
class H5GameController extends BaseController
{
    /**
     * @Route("/")
     */
    public function indexAction()
    {
        $request = $this->get('request');

        $sort = $request->get('sort', '');
        $rows = max(1, $request->get('rows', 10));
        $page = max(1, $request->get('page', 1));

        if (strpos($sort, '_')!==false) {
            list ($sortField, $sortType) = explode('_', $sort, 2);
            $sortType = strtoupper($sortType) =='ASC' ? 'ASC' : 'DESC';

            switch ($sortField) {
                case 'tid'    :
                case 'publish':
                    $orderBy = " ORDER BY h5.{$sortField} {$sortType},h5.logtime DESC";
                    break;
                default:
                    $orderBy = " ORDER BY h5.logtime {$sortType}";
                    break;
            }
        } else {
            $orderBy = " ORDER BY h5.logtime DESC";
        }

        $em = $this->getDoctrine()->getManager();
        $cq = $em
            ->createQuery("SELECT h5,t FROM CmsBundle:h5game h5 JOIN h5.term t WHERE h5.deltime=0".$orderBy)
            ->setFirstResult(($page-1) * $rows)
            ->setMaxResults($rows);

        $records = count(new Paginator($cq, true));
        $h5game  = $cq->getArrayResult();

        return [
            'h5game'  => $h5game,
            'records' => $records,
            'page'    => $page,
            'total'   => ceil($records/$rows),
            'sort'    => $sort,
        ];
    }

    /**
     * @Route("/edit")
     */
    public function editAction()
    {
        $request = $this->get('request');
        $submit  = $request->get('submit');
        $pid     = $request->get('pid', 0);
        $sort = $request->get('sort', '');
        $page = max(1, $request->get('page', 1));

        //跳转URL
        $backUrl = "/cms/h5game/?sort={$sort}&page={$page}";

        $em = $this
            ->getDoctrine()
            ->getManager();

        $current = $pid>0 ? $em->find('CmsBundle:h5game', $pid) : new h5game();

        if ($submit) {
            $tid = $request->get('tid');
            $name  = $request->get('name');
            $link  = $request->get('link');
            $cover = $request->get('cover');
            $intro = $request->get('intro');
            $play  = $request->get('play');
            $score = $request->get('score');
            $printScreen = $request->get('printscreen');
            $publish = $request->get('publish');
            //
            $ot = $em->find('CmsBundle:term', $tid);
            if (!$ot) {
                $this->msgOut('分类不存在！', $backUrl);
            }

            $current
                ->setTerm($ot)
                ->setName($name)
                ->setLink($link)
                ->setCover($cover)
                ->setIntro($intro)
                ->setPlay($play)
                ->setScore(ceil($score))
                ->setPrintScreen((array)$printScreen)
                ->setPublish($publish ? 1 : 0)
                ->setDeltime(0);

            if ($pid<=0) {
                $current->setLogtime(time());
            }

            $em->persist($current);
            $em->flush();

            if ($pid>0) {
                return $this->msgOut('修改游戏成功!', $backUrl);
            }
            return $this->msgOut('添加游戏成功！', $backUrl);
        }

        $ots = $em
            ->createQuery("SELECT t FROM CmsBundle:term t WHERE SUBSTRING(t.identify, 1, 6)=?1")
            ->setParameter(1, 'h5game')
            ->getResult();

        $term = [];
        $this->enumTerm($term, $ots, 0, -1);

        $timestamp = time();
        $token = md5('unique_salt' . $timestamp);

        return [
            'current'  => $current,
            'term'     => $term,
            'timestamp'=> $timestamp,
            'token'    => $token,
            'page'     => $page,
            'sort'     => $sort,
        ];
    }

    /**
     * @Route("/delete")
     */
    public function deleteAction()
    {
        return array(
                // ...
            );    }


    /**
     * @Route("/recycle")
     */
    public function recycleAction()
    {
        return array(
                // ...
            );    }

    /**
     * @Route("/undo")
     */
    public function undoAction()
    {
        return array(
                // ...
            );    }

    /**
     * @Route("/delete2")
     */
    public function delete2Action()
    {
        return array(
            // ...
        );    }

}
